﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace TwoPageView3
{
    delegate void TilePanelTileItemClicked(TiledImageItem clickedItem);

    class TilePanelControl : IVirtualTileEvents
    {
        private List<TiledImageItem> mTileItems = new List<TiledImageItem>();
        private Size mTileSizeDefault = new Size(256, 256);
        private Size mTileSize        = new Size(256, 256);
        private VirtualizingTilePanel mPanel;
        private UserPreference mUserPreference;
        private TilePanelTileItemClicked mItemClickedListener;

        public TilePanelControl(VirtualizingTilePanel panel, UserPreference userPreference, TilePanelTileItemClicked itemClickedListener) {
            mPanel = panel;
            mUserPreference = userPreference;

            ChangeDefaultTileSizeInDevicePixel(new Size(userPreference.ThumbnailSize, userPreference.ThumbnailSize));
            mItemClickedListener = itemClickedListener;
        }

        public void ConnectTo(ThumbnailCache tc) {
            TiledImageItem.ConnectTo(tc);
        }

        public void UpdateFlowDirection(FlowDirection flowDirection) {
            mPanel.FlowDirection = flowDirection;
        }

        /// <summary>
        /// change default tile size (by user preference)
        /// </summary>
        public void ChangeDefaultTileSizeInDevicePixel(Size sizeInDevicePixel) {
            mTileSize = Utility.DevicePixelToSize(mPanel, sizeInDevicePixel);
            mTileSizeDefault = mTileSize;

            TiledImageItem.UpdateDefaultTileSize(mTileSize, sizeInDevicePixel);
            mPanel.UpdateTileSize(mTileSize);
        }

        /// <summary>
        /// change tile size displayed according to individual tile size
        /// </summary>
        public void UpdateTileSize(Size size) {
            mTileSize = size;

            //TiledImageItem.UpdateDefaultTileSize(mTileSize, Utility.SizeToDevicePixel(mPanel, mTileSize));
            mPanel.UpdateTileSize(mTileSize);
            mPanel.UpdateItemPosition();
        }

        public void ClearAllItems() {
            UpdateTileSize(mTileSizeDefault);
            mTileItems.Clear();
            mPanel.ClearChildren();
        }

        public void Add(TpvFileInfo content, CancellationToken token) {
            var tileItem = new TiledImageItem(content, this, token);

            mPanel.AddVirtualChild(tileItem);
            mTileItems.Add(tileItem);
        }

        public void UpdateItemPosition() {
            mPanel.UpdateItemPosition();
        }

        void OnTileItemClicked(object sender, MouseButtonEventArgs e) {
            Console.WriteLine("OnTileItemClicked");
            // sender is Image
            // N.B. e.Source is BitmapImage, not Image
            var image = sender as Image;
            var item = image.Tag as TiledImageItem;
            mItemClickedListener(item);
        }

        void IVirtualTileEvents.OnVitualTileVisualShow(TiledImageItem item) {
            item.Visual.MouseLeftButtonUp += OnTileItemClicked;
            var size = mPanel.MeasureOptimalTileSize();
            UpdateTileSize(size);
        }

        void IVirtualTileEvents.OnVitualTileVisualDispose(TiledImageItem item) {
        }
    }
}
